package com.sinosoft.dsp.persistence.xml.facade;

import java.io.InputStream;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.NoneScoped;

import com.sinosoft.builder.config.BusinessConceptHelper;
import com.sinosoft.builder.utils.BusinessConceptDomainType;
import com.sinosoft.dsp.persistence.businessconcept.facade.IDomainFacade;
import com.sinosoft.dsp.persistence.exception.SinoPersistenceException;
import com.sinosoft.dsp.platform.services.BussinessConceptService;
import com.sinosoft.dsp.system.core.exception.SinoFileException;
import com.sinosoft.dsp.system.core.exception.SinoXMLException;
import com.sinosoft.dsp.system.core.property.BasePropertyHandler;
import com.sinosoft.dsp.system.core.vfs.SinoVFSProxy;
import com.sinosoft.dsp.system.core.vfs.VFSParameters;

/**
 * 信息模型的XML文件形式的操作服务门面实现类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-4-7 上午09:45:56
 */
@ManagedBean(name = "businessConceptXMLServiceFacade")
@NoneScoped
public class BusinessConceptXMLServiceFacade {

	public static final VFSParameters VFS_PARAMETERS = new VFSParameters();

	@ManagedProperty(value = "#{bussinessConceptService}")
	BussinessConceptService bc;

	/**
	 * 获得领域实体存储的相对路径
	 * 
	 * @param ect
	 * @return
	 * @throws SinoPersistenceException
	 */
	private String getDomainDataRelativePath(String conceptId)
			throws SinoPersistenceException {
		BusinessConceptDomainType type = bc.getDomainType(conceptId);
		if (type == null) {
			throw new SinoPersistenceException("通过概念ID无法获得一个合法的领域类型");
		}
		return BasePropertyHandler.getProperty("vfs.repository.path.domain")
				+ "/" + BusinessConceptHelper.getPrefix() + "/"
				+ type.getRelativePath();
	}

	public boolean createFile(IDomainFacade to) throws SinoPersistenceException {
		return createFile(VFS_PARAMETERS, to);
	}

	public boolean createFile(VFSParameters vp, IDomainFacade to)
			throws SinoPersistenceException {
		try {
			return SinoVFSProxy.getInstance().createFile(vp,
					getDomainDataRelativePath(to.getConceptId()), to.getId(),
					to.getXML());
		} catch (SinoFileException e) {
			throw new SinoPersistenceException("创建实体数据XML字符串对象出现错误", e);
		} catch (SinoXMLException e) {
			throw new SinoPersistenceException("创建实体数据XML字符串对象出现错误", e);
		}
	}

	public boolean deleteFile(String conceptId, String uuid)
			throws SinoPersistenceException {
		return deleteFile(VFS_PARAMETERS, conceptId, uuid);

	}

	public boolean deleteFile(VFSParameters vp, String conceptId, String uuid)
			throws SinoPersistenceException {
		try {
			return SinoVFSProxy.getInstance().deleteFile(vp,
					getDomainDataRelativePath(conceptId), uuid);
		} catch (SinoFileException e) {
			throw new SinoPersistenceException(e);
		}

	}

	public boolean editFile(IDomainFacade to) throws SinoPersistenceException {
		return editFile(VFS_PARAMETERS, to);
	}

	public boolean editFile(VFSParameters vp, IDomainFacade to)
			throws SinoPersistenceException {
		try {
			return SinoVFSProxy.getInstance().editFile(vp,
					getDomainDataRelativePath(to.getConceptId()), to.getId(),
					to.getXML());
		} catch (SinoFileException e) {
			throw new SinoPersistenceException("修改实体数据的XML字符串对象出现错误", e);
		} catch (SinoXMLException e) {
			throw new SinoPersistenceException("修改实体数据的XML字符串对象出现错误", e);
		}
	}

	public InputStream readFile(String conceptId, String uuid)
			throws SinoPersistenceException {
		return readFile(VFS_PARAMETERS, conceptId, uuid);
	}

	public InputStream readFile(VFSParameters vp, String conceptId, String uuid)
			throws SinoPersistenceException {
		try {
			return SinoVFSProxy.getInstance().readFile(vp,
					getDomainDataRelativePath(conceptId), uuid);
		} catch (SinoFileException e) {
			throw new SinoPersistenceException(e);
		}
	}

}
